package cn.b.sky.mes.andon.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.andon.dao.AndonAccountDao;
import cn.b.sky.mes.andon.enums.AndonTypeStatus;
import cn.b.sky.mes.andon.model.AndonAccount;
import cn.b.sky.mes.andon.model.condition.AndonAccountCondition;
import cn.b.sky.mes.andon.service.AndonAccountService;
import cn.b.sky.common.utils.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;

@Service("AndonAccountService")
public class AndonAccountServiceImpl implements AndonAccountService{

 private static final Logger LOG = LoggerFactory.getLogger(AndonTypeServiceImpl.class);
	  
	@Resource
	AndonAccountDao andonAccountDao;
	
	@Override
	public List<AndonAccount> selectAndonAccountListByCondition(
			AndonAccountCondition condition) {
		if (null==condition) {
			return null;			
		} 
		if (StringUtils.isEmpty(condition.getOrderField())) {
			condition.setOrderField("CreateDateTime");
		}
		return andonAccountDao.selectAndonAccountListByCondition(condition);
	}

	@Override
	public int selectAndonAccountCountByCondition(
			AndonAccountCondition condition) {
		if (null==condition) {
			return 0;
		}
		return andonAccountDao.selectAndonAccountCountByCondition(condition);
	}

	@Override
	public AndonAccount getAndonAccount(String siteCode, String andonCode) {
		 if (StringUtils.isEmpty(andonCode)) {
		      return null;
		    }
		    return andonAccountDao.getAndonAccount(siteCode,andonCode);
		
	}

	@Override
	public void insertAndonAccount(AndonAccount andonAccount) {
		if(null!=andonAccount){
			andonAccountDao.insertAndonAccount(andonAccount);
		}
		
	}

	@Override
	public void updateAndonAccount(AndonAccount andonAccount) {
		if(null!=andonAccount){
			andonAccountDao.updateAndonAccount(andonAccount);
		}
		
		
	}

	@Override
	public String deleteAndonAccount(String siteCode, String code) {
		 String message = null;    
		    try {
		      andonAccountDao.deleteAndonAccount(siteCode, code);
		    } catch (Exception e) {
		    	 LOG.error("{}", e);
		      if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
		        message="dataBaseOperationPkFail.M";
		      }
		    }
		    return message;
	}

	@Override
	public String disableAndonAccount(String siteCode, String code) {
		 String message = null;    
	    AndonAccount andonAccount=andonAccountDao.getAndonAccount(siteCode, code);	    
	    if (andonAccount.getLockFlag()==Integer.parseInt(AndonTypeStatus.DISABLE.getValue())) {
	      message="StatusIsDisable.M";
	    } else {
	    	andonAccount.setLockFlag(Integer.parseInt(AndonTypeStatus.DISABLE.getValue()));
	      andonAccountDao.updateAndonAccountStatus(andonAccount);
	    }   
	    return message;
	}

	@Override
	public String enableAndonAccount(String siteCode, String code) {
		 String message = null;    
		  AndonAccount andonAccount=andonAccountDao.getAndonAccount(siteCode, code);
		    if (andonAccount.getLockFlag()==Integer.parseInt(AndonTypeStatus.ENABLE.getValue())) {
		      message="StatusIsEnable.M";
		    } else {
		    	andonAccount.setLockFlag(Integer.parseInt(AndonTypeStatus.ENABLE.getValue()));
		      andonAccountDao.updateAndonAccountStatus(andonAccount);
		    }   
		    return message;		
	}

  @Override
  public List<AndonAccount> getMaterialCodeByAndonType(String siteCode, String andonTypeCode) {    
    return andonAccountDao.getMaterialCodeByAndonType(siteCode,andonTypeCode);
  }

}
